package com.mccken.practice.company.duxiaoman;

import java.util.Stack;

/**
 * https://leetcode.cn/problems/valid-parentheses/?company_slug=duxiaoman
 *
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2024-04-01 21:30
 **/
public class Test_20 {

	public boolean isValid1(String s) {

		Stack<Character> stack = new Stack<>();
		for (char c : s.toCharArray()) {
			if (c == '{' || c == '(' || c == '[') {
				stack.push(c);
			} else {
				if (stack.empty()) {
					return false;
				}

				Character peek = stack.pop();

				if ((peek == '(' && c != ')')
						|| (peek == '{' && c != '}')
						|| (peek == '[' && c != ']')) {
					return false;
				}
			}
		}
		return stack.isEmpty();
	}

	public static void main(String[] args) {
		Test_20 test = new Test_20();
	}

}
